<?php

/**
 * 供应商供应商
 */

namespace app\admin\service;

use app\admin\model\SellerInfo;
use think\Service;
use app\admin\model\Supplier as SupplierModel;

class Supplier extends BaseService
{

    /**
     * 获取供应商列表
     * @param $param
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($param = [], $page = 1, $limit = 10)
    {
        $where = [['is_del', '=', 0]];
        $arrData = ['code' => 0, 'message' => 'success'];
        if ($param['supname']) {
            $where[] = ['supname', 'like', '%' . $param['supname'] . '%'];
        }
        if ($param['sup_type']) {
            $where[] = ['sup_type', '=', $param['sup_type']];
        }
        if ($param['phone']) {
            $where[] = ['phone', '=', $param['phone']];
        }
        if ($param['email']) {
            $where[] = ['email', '=', $param['email']];
        }
        if ($param['qq']) {
            $where[] = ['qq', '=', $param['qq']];
        }
        if ($param['wechat']) {
            $where[] = ['wechat', '=', $param['wechat']];
        }
        if ($param['whatsapp']) {
            $where[] = ['whatsapp', '=', $param['whatsapp']];
        }
        if ($param['dingtalk']) {
            $where[] = ['dingtalk', '=', $param['dingtalk']];
        }
        $oSupplierModel = SupplierModel::where($where);
        $total = $oSupplierModel->count();
        $result = $oSupplierModel
            ->with(['get_creator_name', 'get_update_name', 'get_country_value', 'get_owner_value'])
            ->append(['suptype_value', 'isdeal_value', 'partner_value'])
            ->page($page, $limit)
            ->order('id', 'desc')
            ->select();
        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }

    /**
     * 删除单条或多条供应商数据
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($param)
    {
        $where = [['is_del', '=', '0'], ['id', 'in', $param['ids']]];
        $oSupplierModel = SupplierModel::where($where);
        $brand = $oSupplierModel->find();
        if (!$brand) {
            return ['code' => 1, 'message' => '获取供应商信息失败！'];
        }
        $result = $oSupplierModel->update(['is_del' => time()]);
        if ($result) {
            $this->logger->logOperation($param['ids'], 'Supplier', '删除', '删除单条或多条供应商数据');
            return ['code' => 0, 'message' => '删除供应商成功！'];
        } else {
            return ['code' => 1, 'message' => '删除供应商失败！'];
        }
    }

    /**
     * 添加修改供应商
     * @param $param
     * @return array|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save($param)
    {
        $checkSupplierModel = SupplierModel::where([['supname', '=', $param['supname']], ['is_del', '=', 0]]);
        if ($param['id']) {
            $checkSupplierModel->where('id', '<>', $param['id']);
        }
        if ($checkSupplierModel->find()) {
            return ['code' => 1, 'message' => '供应商名称重复！'];
        }

        if ($param['id']) {
            $oSupplierModel = SupplierModel::find($param['id']);
            $oSupplierModel->updater = $param['operator_id'];
            $oSupplierModel->update_time = date('Y-m-d H:i:s', time());
        } else {
            $oSupplierModel = new SupplierModel();
            $oSupplierModel->creator = $param['operator_id'];
            $oSupplierModel->create_time = date('Y-m-d H:i:s', time());
            $oSupplierModel->is_del = 0;
        }

        $oSupplierModel->supname = $param['supname'];
        $oSupplierModel->sup_type = $param['sup_type'];
        $oSupplierModel->image = $param['image'];
        $oSupplierModel->sort_order = $param['sort_order'];
        $oSupplierModel->description = $param['description'];
        $oSupplierModel->phone = $param['phone'];
        $oSupplierModel->contact = $param['contact'];
        $oSupplierModel->country = $param['country'];
        $oSupplierModel->email = $param['email'];
        $oSupplierModel->qq = $param['qq'];
        $oSupplierModel->wechat = $param['wechat'];
        $oSupplierModel->whatsapp = $param['whatsapp'];
        $oSupplierModel->dingtalk = $param['dingtalk'];
        $oSupplierModel->address = $param['address'];
        $oSupplierModel->tags = $param['tags'];
        $oSupplierModel->att_path = $param['att_path'];
        $oSupplierModel->remark = $param['remark'];
        $oSupplierModel->owner = $param['owner'];
        $oSupplierModel->is_deal = $param['is_deal'];
        $oSupplierModel->partner = is_array($param['partner']) ? implode(',', $param['partner']) : $param['partner'];

        if ($oSupplierModel->save()) {
            $this->logger->logOperation($oSupplierModel->id, 'Supplier', '添加修改', '添加/修改供应商');
            return ['code' => 0, 'message' => '保存供应商成功！'];
        }
    }

    /**
     * 获取单个供应商信息
     * @param int $id 供应商ID
     * @return array
     */
    public function getInfo($param)
    {
        $where = [['is_del', '=', '0'], ['id', '=', $param['id']]];
        $oSupplierModel = SupplierModel::where($where)->with(['get_creator_name', 'get_update_name', 'get_country_value', 'get_owner_value'])
            ->append(['suptype_value', 'isdeal_value','partner_value'])->find();
        if (!$oSupplierModel) {
            return ['code' => 1, 'message' => '获取供应商信息失败！'];
        }
        $data = $oSupplierModel->toArray();
        if ($data['partner']) {
            $partnerArr = explode(',', $data['partner']);
            $data['partner'] = array_map(function ($item) {
                return intval($item);
            }, $partnerArr);
        }
        return ['code' => 0, 'message' => 'success', 'data' => $data];
    }

    public function getFieldEnum($field)
    {
        $model = new SupplierModel();
        $res = $model->getFieldEnum($field);
        return [
            'code' => 0,
            'data' => $res
        ];
    }

    /**
     * 获取下拉框选项
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSelect()
    {
        $where = [['is_del', '=', 0]];
        $arrData = ['code' => 0, 'message' => 'success'];

        $where[] = [['category','in',[1,3]]];
        $oSellerInfo = SellerInfo::where($where);
        $total = $oSellerInfo->count();
        $result = $oSellerInfo->field('id AS value,name as label')->select();
        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }
}
